/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.server.util.tracer;

import cn.ac.iie.di.ban.news.server.util.HttpUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;

/**
 *
 * @author zwlin
 */
public class PostDataTracer implements DataTracer {

    ArrayBlockingQueue<String> urls;
    String scope;

    public PostDataTracer(String dataServerType, String url) {
        this.urls = new ArrayBlockingQueue<>(2);
        urls.add(url);
        this.scope = dataServerType;
    }

    public PostDataTracer(String dataServerType, Collection<String> urls) {
        this.urls = new ArrayBlockingQueue<>(urls.size() + 1, false, urls);
        this.scope = dataServerType;
    }

    @Override
    public void traceJsonParseError(String key, String content, long recvTimeSec) throws Exception {
        JSONObject trackJson = new JSONObject();
        trackJson.put(SCOPE_FIELD_NAME, scope);
        trackJson.put(ID_FIELD_NAME, key);
        trackJson.put(MSG_TYPE_FIELD_NAME, "unknown");
        trackJson.put(ERR_TYPE_FIELD_NAME, ERR_TYPE.JSON_ERROR.getType());
        trackJson.put(CONTENT_FIELD_NAME, content);
        trackJson.put(GET_TIME_FIELD_NAME, recvTimeSec);
        trackJson.put(ERR_COUNT_FIELD_NAME, 1);
        trackJson.put(TOTAL_COUNT_FIELD_NAME, 1);
        trace(trackJson);
    }

    @Override
    public void traceMD5Error(String key, String content, long recvTimeSec) throws Exception {
        JSONObject trackJson = new JSONObject();
        trackJson.put(SCOPE_FIELD_NAME, scope);
        trackJson.put(ID_FIELD_NAME, key);
        trackJson.put(MSG_TYPE_FIELD_NAME, "unknown");
        trackJson.put(ERR_TYPE_FIELD_NAME, ERR_TYPE.MD5_ERROR.getType());
        trackJson.put(CONTENT_FIELD_NAME, content);
        trackJson.put(GET_TIME_FIELD_NAME, recvTimeSec);
        trackJson.put(ERR_COUNT_FIELD_NAME, 1);
        trackJson.put(TOTAL_COUNT_FIELD_NAME, 1);
        trace(trackJson);
    }

    @Override
    public void traceCorrect(String key, JSONObject srcPac, JSON trackRecList, int totalCount, long recvTimeSec) throws Exception {
        JSONObject trackJson = new JSONObject();
        trackJson.put(SCOPE_FIELD_NAME, scope);
        trackJson.put(ID_FIELD_NAME, key);
        trackJson.put(MSG_TYPE_FIELD_NAME, srcPac.get("msg_type"));
        trackJson.put(ERR_TYPE_FIELD_NAME, ERR_TYPE.NO_ERROR.getType());
        trackJson.put(CONTENT_FIELD_NAME, trackRecList);
        trackJson.put(GET_TIME_FIELD_NAME, recvTimeSec);
        trackJson.put(ERR_COUNT_FIELD_NAME, 0);
        trackJson.put(TOTAL_COUNT_FIELD_NAME, totalCount);
        trace(trackJson);
    }

    @Override
    public void traceJsonCheckError(String key, JSONObject srcPac, JSON trackRecList, int totalCount, int errorCount, long recvTimeSec
    ) throws Exception {
        JSONObject trackJson = new JSONObject();
        trackJson.put(SCOPE_FIELD_NAME, scope);
        trackJson.put(ID_FIELD_NAME, key);
        trackJson.put(MSG_TYPE_FIELD_NAME, srcPac.get("msg_type"));
        trackJson.put(ERR_TYPE_FIELD_NAME, ERR_TYPE.CHECK_ERROR.getType());
        trackJson.put(CONTENT_FIELD_NAME, trackRecList);
        trackJson.put(GET_TIME_FIELD_NAME, recvTimeSec);
        trackJson.put(ERR_COUNT_FIELD_NAME, errorCount);
        trackJson.put(TOTAL_COUNT_FIELD_NAME, totalCount);
        trace(trackJson);
    }

    private void trace(JSONObject json) throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("type", "track");
        map.put("data", json.toJSONString());
        for (int i = 0; i < urls.size(); ++i) {
            try {
                String url = urls.take();
                urls.put(url);
                url = url + "api_send_testdata";
                HttpUtils.httpPost(url, map);
                return;
            } catch (Exception e) {
            }
        }
        String url = urls.take();
        urls.put(url);
        HttpUtils.httpPost(url, map);
    }

    @Override
    public String getTracerDetail() {
        return "post tracer";
    }
}
